<template>
	<el-form ref="loginForm" :model="form" :rules="rules" label-width="0" size="large">
		<el-form-item prop="phone">
			<el-input
				v-model="form.phone"
				prefix-icon="el-icon-iphone"
				clearable
				:placeholder="$t('login.mobilePlaceholder')"
			>
				<template #prepend>+86</template>
			</el-input>
		</el-form-item>
		<el-form-item prop="yzm" style="margin-bottom: 35px">
			<div class="login-msg-yzm">
				<el-input
					v-model="form.yzm"
					prefix-icon="el-icon-unlock"
					clearable
					:placeholder="$t('login.smsPlaceholder')"
				></el-input>
				<el-button :disabled="disabled" @click="getYzm"
					>{{ this.$t('login.smsGet') }}<span v-if="disabled"> ({{ time }})</span></el-button
				>
			</div>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" style="width: 100%" :loading="islogin" round @click="login">{{
				$t('login.signIn')
			}}</el-button>
		</el-form-item>
		<div class="login-reg">
			{{ $t('login.noAccount') }} <router-link to="/user_register">{{ $t('login.createAccount') }}</router-link>
		</div>
	</el-form>
</template>

<script>
import json from '../../../mock/menu.json'
import jwt_decode from 'jwt-decode'

export default {
	data() {
		return {
			form: {
				phone: '',
				yzm: ''
			},
			rules: {
				phone: [{ required: true, message: this.$t('login.mobileError') }],
				yzm: [{ required: true, message: this.$t('login.smsError') }]
			},
			disabled: false,
			time: 0,
			islogin: false
		}
	},
	mounted() {},
	methods: {
		async getYzm() {
			var validate = await this.$refs.loginForm.validateField('phone').catch(() => {})
			if (!validate) {
				return false
			}

			this.$message.success(this.$t('login.smsSent'))
			this.disabled = true
			this.time = 60
			var t = setInterval(() => {
				this.time -= 1
				if (this.time < 1) {
					clearInterval(t)
					this.disabled = false
					this.time = 0
				}
			}, 1000)
		},
		async login() {
			let user = {
				"data": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIyMDU3MjQ4OSIsImVtcElkIjoiMjA1NzI0ODkiLCJzdGFmZk51bWJlciI6IjIwNTcyNDg5IiwiZXhwIjoxNzU2NTYxNDUzLCJ1c2VyTmFtZSI6IuS5lOWtpum5jyIsImlhdCI6MTc1NjU1OTY1M30.jhvw3YUUhM-jZzwXeaBUX7Z4CA29veb4V-hIkmbwqso",
				"sid": null,
				"success": true,
				"code": "0",
				"message": "操作成功"
			}
			this.$TOOL.cookie.set('TOKEN', user.data, {
				expires: this.form.autologin ? 24 * 60 * 60 : 0
			})
			// console.log(user.data)
			// this.$TOOL.data.set('USER_INFO', user.data.userInfo)
			const decodeToken = jwt_decode(user.data).userName
			this.$TOOL.data.set('USER_INFO', { userName: decodeToken })

			let menu = json;
			let defaultUrl = ''
			if (menu.code == 0) {
				if (menu.data.menu.length === 0) {
					this.islogin = false
					this.$alert('当前用户无任何菜单权限，请联系系统管理员', '无权限访问', {
						type: 'error',
						center: true
					})
					return false
				}

				// if(permissions.code == 0){
				this.$TOOL.data.set('PERMISSIONS', menu.data.permissions)
				// }
				this.$TOOL.data.set('MENU', menu.data.menu)
				defaultUrl = menu.data.url

				// menu.data.permissions = [ 'activity-direct-totality','activity-direct-product',
				// 'activity-overview-direct-area','activity-overview-direct-client','activity-KA-totality','activity-overview-KA-product',
				// 'activity-overview-KA-area','activity-overview-KA-client',
				// ]
			} else {
				this.islogin = false
				this.$message.warning(menu.message)
				return false
			}


			console.log(defaultUrl)
			if(!defaultUrl){
				this.$router.replace({
					path: '/'
				})
			}else{
				this.$router.replace({
					path: defaultUrl
				})
			}

			this.$message.success('Login Success 登录成功')
		}
	}
}
</script>

<style></style>
